<?php

class Setting extends MY_Controller {

	function __construct()
	{
		parent::__construct();		
		
		$this->page=$this->module_codes['Settings'];
		$this->load->model('setting_model' , 'SM');
	}
	
	function index()
	{
		
        $this->check_page_permission('access');
		$this->load->view('head');
		$this->load->view('setting/main');
		$this->load->view('footer');
	}
	function saveReportSetting(){
		$this->check_page_permission('edit');
		$report_form = $this->input->post('report_form');
		
		$this->load->model('setting_model', 'SM');
		$attachSetting= $this->SM->getSetting('Attachment');
		
		$upload_path='./images/';
		
		if(!is_dir($upload_path))mkdir($upload_path) ;
		$config['upload_path'] = $upload_path;
		$config['allowed_types'] =$attachSetting['types'];
		$config['max_size']	= $attachSetting['max_size']*1024;

		$this->load->library('upload', $config);

		if ( $this->upload->do_upload('logo'))
		{
			$data = array('upload_data' => $this->upload->data());
			$report_form['logo']=$data['upload_data']['file_name'];
			//print_r($data);
			//$this->load->view('patient/upload_success', $data);
		}else
		print_r($this->upload->display_errors());
		
		
		
		if(!empty($report_form)){
	 	 $this->SM->saveSetting('PDF',$report_form);
			 
		}
      $this->general();		
	}
    function saveAddressSetting(){
		$this->check_page_permission('edit');
		$address_form = $this->input->post('address_form');
		if(!empty($address_form)){
	 	 $this->SM->saveSetting('Address',$address_form);
			 
		}
      $this->general();		
	}
function saveAttachmentSetting(){
		$this->check_page_permission('edit');
		$attachment_form = $this->input->post('attachment_form');
		if(!empty($attachment_form)){
	 	 $this->SM->saveSetting('Attachment',$attachment_form);
			 
		}
      $this->general();		
	}
	
function getReportSetting(){
		return $this->SM->getSetting('PDF');
		
	}
	function getAdressSetting(){
		return $this->SM->getSetting('Address');
		
	}
	function getAttachmentSetting(){
		return $this->SM->getSetting('Attachment');
		
	}
    function DB_backup(){
    	
       $this->check_page_permission('access');
    	// Load the DB utility class
		$this->load->dbutil();
		$prefs = array(
                'format'      => 'txt',
                'filename' => date('Y-m-d h:i'),
                'add_drop'    => TRUE,              // Whether to add DROP TABLE statements to backup file
                'add_insert' => TRUE, // Whether to add INSERT data to backup file
              );
		// Backup your entire database and assign it to a variable
		$backup =& $this->dbutil->backup($prefs);
		
		// Load the file helper and w$prefse the file to your server
#		print_r($backup);
		$this->load->helper('file');

		$backup_file='./Backups/DB/'.date('Y-m-d h:i').'.txt';
		write_file($backup_file, $backup);

		$backup_file='./backups/db/'.date('Y-m-d h:i').'.txt';
		
	    write_file($backup_file, $backup,'a+');
	

		/*
			$config = Array(
			  'protocol'    => 'smtp',
                          'smtp_user'   => 'med-dots-mail@dotssol.com@dotssol.com',
			  'smtp_host'   => 'smtp.dotssol.com',
                          'smtp_pass'   => '12341234',
			  'smtp_port'   => 587
			);
			$this->load->library('email', $config);
			
			
		$this->email->from('med-dots-mail@dotssol.com', 'Administrator [MED-DOTs DB Backup]');
		$this->email->to('mhkonly@gmail.com');
		
		
		$this->email->subject('DB backup');
		$this->email->message('Find in the attached the DB backup for'.date('Y-m-d h:i'));
		//$this->email->attach($backup_file);
		if(!$this->email->send())
                die($this->email->print_debugger());
		*/
		// Load the download helper and send the file to your desktop
		$this->load->helper('download');
		force_download($backup_file, $backup); 
    }

	function general()
	{
		$this->check_page_permission('access');
		$reportSetting=$this->getReportSetting();
		$addressSetting=$this->getAdressSetting();
		$attachSetting=$this->getAttachmentSetting();
		$this->load->view('head');
		$this->load->view('setting/general',array_merge($reportSetting,$addressSetting,$attachSetting));
		$this->load->view('footer');
	}
	
}

/* End of file setting.php */
/* Location: ./system/application/controllers/setting.php */
